﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SynapseGaming.LightingSystem.Lights;
using SynapseGaming.LightingSystem.Shadows;
using Microsoft.Xna.Framework;

namespace The_LarX
{
    public class DirectionalLightSunBurn : Component
    {
        public DirectionalLight DirectionalLight;
        public Actor Actor;
        public Renderer Renderer;

        public DirectionalLightSunBurn(Renderer renderer, GameScreen screen)
            : base(screen)
        {
            this.Renderer = renderer;
            DirectionalLight = new DirectionalLight();
            Actor = new Actor("Engine\\Light\\Model\\model_light");
            Actor.Parent = DirectionalLight;
            Actor.ComponentParent = this;

            DirectionalLight.Direction = new Vector3(-0.46f, -0.87f, -0.17f);
            DirectionalLight.DiffuseColor = new Vector3(1.0f, 0.97f, 0.77f);
            DirectionalLight.Intensity = 1;
            DirectionalLight.ShadowType = ShadowType.AllObjects;
            DirectionalLight.ShadowQuality = 1.0f;
            DirectionalLight.ShadowPrimaryBias = 1.0f;
            DirectionalLight.ShadowSecondaryBias = 0.04f;
        }
        public DirectionalLightSunBurn()
        {
        }

        public override void Update()
        {
            base.Update();

            Actor.Update();
        }

        public override void Draw()
        {
            base.Draw();

            Actor.Draw();
            if (Renderer != null)
            {
                Renderer.SubmitActor(Actor);
                Renderer.SubmitLight(DirectionalLight);
            }
        }

        public override void SerializeData()
        {
            base.SerializeData();

            Data.AddData("Light.Direction", DirectionalLight.Direction);
            Data.AddData("Light.DiffuseColor", DirectionalLight.DiffuseColor);
            Data.AddData("Light.Enabled", DirectionalLight.Enabled);
            Data.AddData("Light.Intensity", DirectionalLight.Intensity);
            Data.AddData("Light.ShadowQuality", DirectionalLight.ShadowQuality);
            Data.AddData("Light.ShadowPrimaryBias", DirectionalLight.ShadowPrimaryBias);
            Data.AddData("Light.ShadowSecondaryBias", DirectionalLight.ShadowSecondaryBias);
            Data.AddData("Light.ShadowType", (int)DirectionalLight.ShadowType);

            Data.AddData("Renderer", this.Renderer.Name);

            Actor.SerializeData(Data);
        }
        public override void DeserializeData()
        {
            base.DeserializeData();

            DirectionalLight = new DirectionalLight();
            DirectionalLight.Intensity = Data.GetData<float>("Light.Intensity");
            DirectionalLight.Direction = Data.GetData<Vector3>("Light.Direction");
            DirectionalLight.DiffuseColor = Data.GetData<Vector3>("Light.DiffuseColor");
            DirectionalLight.Enabled = Data.GetData<bool>("Light.Enabled");
            DirectionalLight.Intensity = Data.GetData<float>("Light.Intensity");
            DirectionalLight.ShadowQuality = Data.GetData<float>("Light.ShadowQuality");
            DirectionalLight.ShadowPrimaryBias = Data.GetData<float>("Light.ShadowPrimaryBias");
            DirectionalLight.ShadowSecondaryBias = Data.GetData<float>("Light.ShadowSecondaryBias");
            DirectionalLight.ShadowType = (ShadowType)Data.GetData<int>("Light.ShadowType");

            Actor = new Actor("Engine\\Light\\Model\\model_light");
            Actor.Parent = DirectionalLight;
            Actor.ComponentParent = this;

            Actor.DeserializeData(Data);

            string rendererName = Data.GetData<string>("Renderer");
            if (!string.IsNullOrEmpty(rendererName))
            {
                if (EngineSystem.Renderers.ContainsKey(rendererName))
                {
                    this.Renderer = EngineSystem.Renderers[rendererName];
                    Renderer.SubmitLight(DirectionalLight);
                    Renderer.SubmitActor(Actor);
                }
            }
            else
            {
                if (EngineSystem.Renderers.Count > 0)
                {
                    this.Renderer = EngineSystem.Renderers.Values.First();
                    Renderer.SubmitLight(DirectionalLight);
                    Renderer.SubmitActor(Actor);
                }
            }

        }
    }
}
